# -- coding: utf-8 --
import json

import urllib3

import helpers.globals as globals
import requests
import time
from sqlalchemy import func, select
import helpers.sql as sql
from sqlalchemy.orm import sessionmaker
import pojo.all_model as all_model







# data_api = 'https://app.online.xiongmaozikao.com/api/v1/getCateList'
#
# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = header)
#
# # response = requests.get(
# #     'https://app.online.xiongmaozikao.com/api/v1/getCateList',
# #     headers = header
# # )
#
# print(api_response.data.decode())








# data_api = domain + '/api/v1/getAreaList'

#
# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = header)
#
# # response = requests.get(
# #     'https://app.online.xiongmaozikao.com/api/v1/getCateList',
# #     headers = header
# # )
#
# print(api_response.data.decode())






# data_api = domain + '/api/v1/getMajorList?layer_id=1&area_id=7&cate_id=1'

# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = header)
#
# # response = requests.get(
# #     'https://app.online.xiongmaozikao.com/api/v1/getCateList',
# #     headers = header
# # )
#
# print(api_response.data.decode())






# data_api = domain + '/api/v1/getExamList?major_id=444'
#
# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = header)
#
# # response = requests.get(
# #     'https://app.online.xiongmaozikao.com/api/v1/getCateList',
# #     headers = header
# # )
#
# print(api_response.data.decode())






# data_api = globals.domain + '/api/v1/getQuestionGroupList?subject_id=79&type=2'
#
# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = globals.header)
#
# # response = requests.get(
# #     'https://app.online.xiongmaozikao.com/api/v1/getCateList',
# #     headers = header
# # )
#
# print(api_response.data.decode())







# data_api = globals.domain + '/api/v1/getQuestionList?id=182982_78_184531_2&num=40&is_view=0'
#
# # 请求接口
# http = urllib3.PoolManager()
# api_response = http.request('GET', data_api, headers = globals.header)
#
# print(api_response.data.decode())

# response = requests.get(
#     globals.domain + globals.subjectApi + '?id=182982_78_184531_2&num=40&is_view=0',
#     headers = globals.header,
#     verify = False
# )
# print(response.text)







def addAllObject(areaJson, objects):
    weigh = 100
    for regionObj in areaJson:
        region = all_model.KaotikuRegion(id=regionObj['id'], pid=regionObj['pid'], weigh=weigh,
                                         name=regionObj['name'], level=regionObj['level'])
        weigh = weigh + 1
        objects.append(region)
        if 'children' in regionObj:
            addAllObject(regionObj['children'], objects)
            continue
    return objects

with open("area.txt", "r", encoding="utf-8") as file:
    # 读取文件内容
    areaJson = json.load(file)

    timestamp = int(time.time())
    engine = sql.getEngine(None, None)
    session = sessionmaker(engine)()
    session.begin()

    objects = []
    try:
        addAllObject(areaJson['data'], objects)

        session.add_all(objects)
        session.commit()
    except Exception as e:
        session.rollback()
        raise e
    finally:
        session.close()
















